package com.pu.controller.user;

import com.pu.dto.OrdersPaymentDTO;
import com.pu.dto.OrdersSubmitDTO;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.OrderService;
import com.pu.vo.OrderPaymentVO;
import com.pu.vo.OrderSubmitVO;
import com.pu.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Yusc
 * @Date 2023/11/1 18:33
 * @Version
 * @Description C端订单管理
 **/
@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * @param ordersSubmitDTO 前端提交的数据
     * @return
     * @auther: Yusc
     * @date 2023/11/2 12:41
     * @Description 用户下单
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * @param page     页码
     * @param pageSize 每页显示数量
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @auther: Yusc
     * @date 2023/11/4 16:34
     * @Description 历史订单查询
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        log.info("历史订单分页查询:page:{},pageSize:{},status:{}", page, pageSize, status);
        PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * @param id 订单id
     * @auther: Yusc
     * @date 2023/11/4 17:39
     * @Description 查询订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        log.info("查询id:{}的订单详情", id);
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    /**
     * @param id 订单id
     * @auther: Yusc
     * @date 2023/11/4 17:49
     * @Description 用户取消订单
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable("id") Long id) throws Exception {
        log.info("取消订单id:{}", id);
        orderService.userCancelById(id);
        return Result.success();
    }

    /**
     * @param id 订单id
     * @auther: Yusc
     * @date 2023/11/4 21:09
     * @Description 再来一单
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id) {
        log.info("再来一单 id:{}", id);
        orderService.repetition(id);
        return Result.success();
    }

    /**
     * @param ordersPaymentDTO 接收前端传递数据
     * @auther: Yusc
     * @date 2023/11/4 23:31
     * @Description 订单支付 微信支付需要商家认证才能开通,所以只能模拟支付
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);

        // 模拟交易成功，直接修改订单状态
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }

    /**
     * @param id 订单id
     * @auther: Yusc
     * @date 2023/11/5 17:06
     * @Description 用户催单
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long id) {
        orderService.reminder(id);
        return Result.success();
    }

}
